package com.das.mechanic_base.widget;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;
import com.kproduce.roundcorners.RoundTextView;

/**
 * Created by chensi on 2018/4/24.
 */

public class X3WorkNoticeDialog extends Dialog {

    private Point mScreenPoint = new Point();
    private Activity mRootActivity = null;

    private TextView tv_title;
    private TextView tv_content;
    private RoundTextView rv_btn;

    /**
     * 一个按钮Dialog
     *
     * @param arg
     */
    public X3WorkNoticeDialog(Activity arg) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = null;
        view = findViewById(viewId);
        return (T) view;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(R.layout.x3_work_notice_dialog);
        initDialogWindow();
        initView();
    }


    void initView() {
        tv_title = findViewById(R.id.tv_title);
        tv_content = findViewById(R.id.tv_content);
        rv_btn = findViewById(R.id.rv_btn);

        rv_btn.setOnClickListener(v -> {
            if (iOnWorkNotice != null) {
                iOnWorkNotice.iOnWorkNoticeDismiss();
            }
            dismiss();
        });
    }

    public void showWorkNotice(boolean isBefore,String content) {
        if (timer != null) {
            timer.start();
        }
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        rv_btn.setBackgroundColor(ContextCompat.getColor(mRootActivity,R.color.bg_F0F0F5));
        rv_btn.setClickable(false);
        if (isBefore) {
            tv_title.setText(mRootActivity.getString(R.string.x3_work_before_title));

        }else {
            tv_title.setText(mRootActivity.getString(R.string.x3_work_after_title));

        }
        tv_content.setText(content);
    }

    IOnWorkNotice iOnWorkNotice;

    public X3WorkNoticeDialog setiOnWorkNotice(IOnWorkNotice iOnWorkNotice) {
        this.iOnWorkNotice = iOnWorkNotice;
        return this;
    }

    public interface IOnWorkNotice {
        void iOnWorkNoticeDismiss();
    }

    private final CountDownTimer timer = new CountDownTimer(4 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            int time = (int) (millisUntilFinished / 1000);
            rv_btn.setText(mRootActivity.getString(R.string.x3_already_know) + "(" + time + "s)");
        }

        @Override
        public void onFinish() {
            if (!isShowing()) {
                return;
            }
            rv_btn.setText(mRootActivity.getString(R.string.x3_already_know));
            rv_btn.setClickable(true);
            rv_btn.setBackgroundColor(ContextCompat.getColor(mRootActivity,R.color.bg_0077FF));
            setCancelable(true);
            setCanceledOnTouchOutside(true);
        }
    };

    @Override
    public void dismiss() {
        if (timer != null) {
            timer.cancel();
        }
        super.dismiss();
    }

    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

        WindowManager m = mRootActivity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.70);    // 宽度设置为屏幕的0.9
        dialogWindow.setAttributes(p);
    }


}


